package org.songwang.org.controller;

import org.apache.ibatis.annotations.Delete;
import org.songwang.basic.AjaxResult;
import org.songwang.basic.PageList;
import org.songwang.org.domain.Department;
import org.songwang.org.query.DepartmentQuery;
import org.songwang.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//a/t+ctrl+t 快捷try，catch
//@GetMapping
//@PostMapping
//@PutMapping
//@DeleteMapping()
@RestController
//@Controller+@ResponseBody等于@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    // 通过get请求  通过id查询  @GetMapping("/{id}")和@PathVariable("id")的id必须一致
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        Department byId = departmentService.findById(1L);
        return byId;
    }
    //分页查询
    @PostMapping
    public PageList queryAll(@RequestBody DepartmentQuery query){
        System.out.println(query);
        return departmentService.findQueryVO(query);
    }
    //添加或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() !=null){
                departmentService.update(department);
            }else {
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        System.out.println(ids);
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    @GetMapping
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
